package org.devocean.groupbuying.utils;

import java.text.Normalizer;
import java.text.Normalizer.Form;

import org.devocean.groupbuying.domain.Contact;
import org.devocean.groupbuying.domain.Deal;

public class UriUtils {

	public UriUtils() {
		// WebApplicationContext ctx = ContextLoader
		// .getCurrentWebApplicationContext();

	}

	/**
	 * Generate a slugified String from a normal String according to a slugfy
	 * rule
	 * 
	 * @param string
	 *            text to slugify
	 * @return slugified text
	 */
	public static String slugify(String string) {
		return Normalizer.normalize(string.toLowerCase(), Form.NFD)
				.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
				.replaceAll("[^\\p{Alnum}]+", "_");
	}

	public static String getTestUri(Contact contact) {
		String uri = "test/" + slugify("test");
		return uri;
	}

	/**
	 * Generate a user friendly Contact uri
	 * 
	 * @param contact
	 *            Contact instance
	 * @return adapted uri : contains Information about Contact object according
	 *         to a Matching rule
	 * @see org.devocean.groupbuying.web.frontend.ContactController
	 * 
	 */
	public static String contactUri(Contact contact) {
		String uri = "" + slugify(contact.getLastname()) + "-"
				+ contact.getId();
		return uri;
	}

	/**
	 * Generate a user friendly Deal uri
	 * 
	 * @param deal
	 *            Deal instance
	 * @return adapted uri : contains Information about deal object according to
	 *         a Matching rule
	 * @see org.devocean.groupbuying.web.frontend.DealController
	 * 
	 */
	public static String dealUri(Deal deal) {
		String uri = "";
		if (deal.getArticlesLocalization()
						.getPermalink() !=null) {
			uri = deal.getArticlesLocalization()
					.getPermalink() + "-"
			+ deal.getDealID();
		}else{
			uri = ""
					+ slugify(deal.getArticlesLocalization().getArticles()
							.iterator().next().getShorte()) + "-"
					+ deal.getDealID();
		}
		
		
		return uri;
	}
}
